/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.fedoraaccess.rest;

import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.rehan.commons.DatastreamId;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import java.io.PrintStream;
import java.util.List;
import org.dom4j.Document;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class RESTConnector implements FedoraConnector {

    private final FedoraServer fedoraServer;

    public RESTConnector(FedoraServer fedoraServer) {
        this.fedoraServer = fedoraServer;
    }

    @Override
    public List<String> findObjects(String termsString) {
        return new FindObjectsByTermsOperation(fedoraServer).run(termsString);
    }

    @Override
    public Document getXmlDatastreamDissemination(String pid, DatastreamId dsId) {
        return new GetXmlDatastreamDisseminationOperation(fedoraServer).run(pid, dsId);
    }

    @Override
    public String importFoxml(Document foxmlDoc, String logMessage) {
        return new IngestFoxmlOperation(fedoraServer).run(foxmlDoc, logMessage);
    }

    @Override
    public boolean objectExists(String objectPid) {
        return new ObjectExistsOperation(fedoraServer).run(objectPid);
    }

    @Override
    public void printRepositoryInfo() {
        printRepositoryInfo(System.out);
    }

    @Override
    public void printRepositoryInfo(PrintStream out) {
        out.println("not implemented");
    }

    @Override
    public void removeObject(String pid, String logMessage) {
        new PurgeObjectOperation(fedoraServer).run(pid, logMessage);
    }

    @Override
    public boolean addRelationship(String subject, String predicate, String predicatePrefix, String object) {
        return (new AddRelationshipFedora3_3Operation(fedoraServer).run(subject, predicate, predicatePrefix, object));
        //TODO: after moved to Fedora 3.4 use REST method again
        //String result = (new AddRelationshipOperation(fedoraServer).run(subject, predicate, object));
        //System.err.println("result: " + result);
    }

    @Override
    public boolean removeRelationship(String subject, String predicate, String predicatePrefix, String object) {
        return (new PurgeRelationshipFedora3_3Operation(fedoraServer).run(subject, predicate, predicatePrefix, object));
        //TODO: after moved to Fedora 3.4 use REST method again
        //new PurgeRelationshipOperation(fedoraServer).run(subject, predicate, object);
    }

    @Override
    public boolean removeRelationships(String subject, String predicate, String predicatePrefix) {
        return (new PurgeRelationshipFedora3_3Operation(fedoraServer).run(subject, predicate, predicatePrefix, null));
        //TODO: after moved to Fedora 3.4 use REST method again
        //new PurgeRelationshipOperation(fedoraServer).run(subject, predicate, object);
    }

    @Override
    public boolean updateDatastreamInlineContent(String pid, DatastreamId dsId, Document content) {
        return (new UpdateDatastreamInlineContentOperation(fedoraServer).run(pid, dsId, content));
    }

    @Override
    public String getRepositoryUrl() {
        return fedoraServer.getUrl();
    }

    @Override
    public Document getObjectFoxml(String pid) {
        return (new GetObjectFoxmlOperation(fedoraServer).run(pid));
    }

    @Override
    public boolean purgeDatastream(String pid, DatastreamId dsId, String logMessage) {
        return (new PurgeDatastreamOperation(fedoraServer).run(pid, dsId, logMessage));
    }

    @Override
    public boolean addExternallyReferencedDatastream(String pid, DatastreamId dsId, String url, String mimeType, String label) {
        return (new AddExternallyReferencedDatastreamOperation(fedoraServer).run(pid, dsId, url, mimeType, label));
    }
}
